<?php
/*!
 * LostKids PHP Library v0.1 
 * http://huali.li/
 *
 * Copyright 2011, 
 * Written By James Pang
 *  
 * Date: 2011/2/14
 */

class MsgSrcPluginSina extends MsgSrcPluginAbs
{
    private $wbClient;

    public function __construct() {
        parent::__construct();
    	$this->wbClient = Assembly::getService('WeiboClientXAuthed', true)->wbClient;
    }

    protected function respondByMsgId($msgId, $content, $withUrl = false) {
        if($withUrl)
            return false;// sina 评论不让带url
        $this->wbClient->send_comment($msgId, $content); // 发评论
    }

    protected function respondByOwnerId($ownerId, $content, $withUrl = false) {
        $this->wbClient->send_dm($ownerId, $content);// 发私信
    }

    const delim = '|';
    protected function getMsgsList($dataSrcKey, $maxCnt, $sinceId) {
        $sinceIds = explode(delim, $sinceId);

        // 优先取回复信息，再取新登记消息
        $page = 300/$maxCnt + 1; // 起始页数
        do {
            // match/confirm msgs from private msgs
            $msgs1 = $this->wbClient->list_dm($page--, $maxCnt, @$sinceIds[0]);
        } while($page > 0 && count($msgs1) == 0);
        if(count($msgs1) > 0) {
        	@$sinceIds[0] = $msgs1[0]['id'];
        }

        $page = 300/$maxCnt + 1; // 起始页数
        do {
            // seek/report msgs
            $msgs2 = $this->wbClient->mentions($page--, $maxCnt, @$sinceIds[1]);
        } while($page > 0 && count($msgs2) == 0);
        if(count($msgs2) > 0) {
            @$sinceIds[1] = $msgs2[0]['id'];
        }

        return array(array_merge($msgs1, $msgs2), implode(delim, $sinceIds));
    }

    protected function getMsgContent($msg) {
        return $msg['text'];
    }

    /**
     * 支持转发
     */
    protected function getImgUrl($msg) {
        if(!@$msg['original_pic']) {
            return null;
        } else {
            $imgUrl = $msg['original_pic'];
        }
        // check retweet
        if(@$msg['retweeted_status']['original_pics']) {
            return null;
        } else if(!@$imgUrl) {
            $imgUrl = $msg['original_pic'];
        }

        return $imgUrl;
    }
}
?>
